//
//  AppDelegate.swift
//  Weibo
//
//  Created by 夏灵烽 on 2025/1/1.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.backgroundColor = .white
        
        window?.rootViewController = WBMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }

    func loadAppInfo() {
        DispatchQueue.global().async {
            let path: String = Bundle.main.path(forResource: "main.json", ofType: nil) ?? ""
            
            var url: URL
            if #available(iOS 16.0, *) {
                url = URL(filePath: path)
            } else {
                url = URL(fileURLWithPath: path)
            }
            
            // 获取文档目录 URL
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            
            var fileURL: URL
            
            // 附加文件名
            if #available(iOS 16.0, *) {
                fileURL = documentsURL.appending(path: "main.json", directoryHint: .notDirectory)
            } else {
                fileURL = documentsURL.appendingPathComponent("main.json")
            }
            
            do {
                let data = try Data(contentsOf: url)
                
                try data.write(to: fileURL)
                
            } catch  {
                print(error)
            }
            
            
        }
    }

}

